<template>
  <div>
    <t-form :data="formData" labelWidth="120" ref="form" :rules="rules" @submit="onSubmit" scrollToFirstError="smooth">
      <div class="top flex-c jc-sb">
        <div class="left flex-c">
          <img src="@/assets/image/addAccount.png" alt="" srcset="">
          <span>默认情况下密码为新增账号名称大写首字母加手机号后六位</span>
        </div>
        <t-form-item>
          <t-space :size="24">
            <t-button theme="default" variant="base">取消</t-button>
            <t-button theme="primary" type="submit">确定</t-button>
          </t-space>
        </t-form-item>
      </div>

      <div class="pageBg mtb-16 content">
        <t-form-item label="新增账号手机号" name="phone">
          <t-input v-model="formData.phone" placeholder="请输入手机号" type="number"></t-input>
        </t-form-item>
        <t-form-item label="新增账号名称" name="name">
          <t-input v-model="formData.name" placeholder="请输入账号名称" :maxlength="10"></t-input>
        </t-form-item>
        <t-form-item label="新增账号角色（可多选）" name="roles">
          <div class="flex-c addRoles">
            <t-checkbox-group v-model="formData.roles" @change="onRolesChange">
              <t-checkbox key="1" label="老板" value="1" />
              <t-checkbox key="2" label="店长" value="2" />
              <t-checkbox key="3" label="前台" value="3" />
              <t-checkbox key="4" label="其他角色" value="4" />
            </t-checkbox-group>
            <t-form-item name="otherRole">
              <t-input v-show="isOtherRples" v-model="formData.otherRole" placeholder="请输入角色"></t-input>
            </t-form-item>
          </div>
        </t-form-item>
        <t-form-item label="授权店铺" name="shop">
          <t-select v-model="formData.shop">
            <t-option key="apple" label="Apple" value="apple" />
            <t-option key="orange" value="orange">Orange</t-option>
            <t-option key="banana" label="Banana" value="banana" />
          </t-select>
        </t-form-item>
      </div>

      <div class="pageBg">
        <div class="pageTitle mb-16">账号权限分配</div>
        <t-form-item name="jurisdiction">

          <t-checkbox-group v-model="formData.jurisdiction">
            <t-checkbox v-for="(item, index) in jurisdictionList" :label="item.label" :value="item.value"
              :key="index" />
          </t-checkbox-group>

        </t-form-item>
      </div>
    </t-form>
  </div>
</template>

<script>
export default {
  name: '',
  data() {
    return {
      formData: {
        phone: "",
        name: "",
        roles: [],
        otherRole: "",
        shop: "",
        jurisdiction: [],
        type: 'add'
      },
      jurisdictionList: [
        {
          label: "工作中心",
          value: "1"
        },
        {
          label: "任务中心",
          value: "2"
        },
        {
          label: "数据中心",
          value: "3"
        },
        {
          label: "经营指南",
          value: "4"
        },
        {
          label: "信息管理",
          value: "5"
        },
        {
          label: "财务管理",
          value: "6"
        },
        {
          label: "评价管理",
          value: "7"
        },
        {
          label: "订单管理",
          value: "8"
        },
        {
          label: "房态房价",
          value: "9"
        },
        {
          label: "系统管理",
          value: "10"
        },
      ],
      rules: {
        phone: [{ required: true, message: '请填写新增账号手机号', type: 'error', trigger: 'blur' }],
        name: [{ required: true, message: '请填写新增账号名称', type: 'error', trigger: 'blur' }],
        roles: [
          { required: true, message: '请选择新增账号角色', type: 'error', trigger: 'blur' },
        ],
        shop: [{ required: true, message: '请选择授权店铺', type: 'error', trigger: 'blur' }],
        otherRole: [{ required: true, message: '请选择新增账号角色', type: 'error', trigger: 'blur' }, { validator: this.rolesValidator }],
      },
      isOtherRples: false
    }
  },
  created() {
    this.type = this.$route.query.type
  },
  methods: {
    onSubmit({ validateResult, firstError }) {
      if (validateResult === true) {
        this.$message.success('提交成功');
      } else {
        console.log('Errors: ', validateResult);
        this.$message.warning(firstError);
      }
    },
    onRolesChange(checked, e) {
      if (e.current === '4') {
        if (e.type == 'uncheck') {
          this.isOtherRples = false
        } else {
          this.isOtherRples = true
        }
      }
    },
    rolesValidator(val) {
      if (this.formData.roles.includes("4") && !val) {
        return { result: true, message: '请输入角色', type: 'error' };
      }
      return { result: true, type: 'error' }
    },
  },
}
</script>
<style lang='less' scoped>
.left {
  padding: 5px 12px;
  background-color: #FFE4BA;
  font-size: 14px;
  color: #FF9A2E;

  img {
    width: 14px;
    height: 14px;
  }

  span {
    margin-left: 12px;
  }
}

.mtb-16 {
  margin: 16px 0;
}

.mb-16 {
  margin-bottom: 16px;
}

.content {
  display: flex;

  ::v-deep .t-form__item {
    margin-right: 24px;
  }

  ::v-deep .t-input__wrap {
    width: 220px;
  }

  .addRoles {
    ::v-deep .t-checkbox-group {

      .t-checkbox {
        width: 80px;
        margin-right: 16px;
      }
    }

  }
}

::v-deep .t-form__controls.t-is-success .t-input__extra {
  color: #d54941;
}

::v-deep .t-checkbox-group {
  width: 100%;
  justify-content: space-between;
}
</style>